/**
 *  实现api 接口 post: /confirm/by/password  body:{password:password}
 */
;layui.define(["jquery", "form", 'layer'], function (exports) {
    "use strict";
    const $ = layui.jquery;
    const form = layui.form;
    const layer = layui.layer;
    let url;
    let callback;
    let $shadow;
    let $container;
    let reloadAfterHide;

    function createEl(idSelector) {


        $shadow = $("<div class='auth-confirm-box'></div>").css({
            zIndex: 999,
            position: 'fixed',
            top: 0,
            left: 0,
            width: '100vw',
            height: '100vh',
            background: 'rgba(0,0,0,0.2)'
        });
        const $form = $('<form class="layui-form"></form>').css({
            position: 'absolute',
            bottom: 0,
            left: '50%',
            transform: 'translate(-50%,0)',
            minWidth: '510px',
            height: 'auto'
        });
        const $formItem = $("<div class='layui-form-item'></div>");
        const $label = $("<label class='layui-form-label'></label>").css({
            fontWeight: 'bold',
            textShadow: '0px 1px 1px',
            letterSpacing: '1px',
            color: '#403837;'
        });
        const $input = $("<div class='layui-input-inline'><input class='layui-input' type='password' name='password' lay-verify='required'/></div>");
        const $submit = $("<div class='layui-input-inline'><button class='layui-btn layui-btn-danger' lay-submit lay-filter='confirmAuth'>校验身份</button></div>");
        const password = $formItem.append($label.html("账户密码：")).append($input).append($submit);
        $container = $(idSelector).append($shadow.append($form.append(password)));
        $shadow.on("click", function (e) {
            if (e.target === e.currentTarget) {
                destroyEl();
            }

        })
    }

    function destroyEl() {
        $shadow.remove();
        if (reloadAfterHide) {
            location.reload();
        }
    }

    function post(url, param) {
        return new Promise((resolve) => {
            $.ajax({
                url: url,
                type: 'POST',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                data: param,
                success: function (res) {
                    resolve(res)
                },
            })

        })
    }

    function confirm(containerId ,success, reloadAfterDestroy) {


        callback = success;
        reloadAfterHide = reloadAfterDestroy;
        createEl(containerId);

    }

    form.on('submit(confirmAuth)', function (data) {

        const password = data.field.password;
        const authRes = post('/auth/confirm', JSON.stringify({password: password}));
        authRes.then(function (res) {

            if (res === 'ok') {

                destroyEl();
                callback();
            } else {
                layer.msg("密码错误，请重新输入", {icon: 3});
            }
        });
        return false;
    })

    exports('authConfirm', function () {
        return {confirm: confirm}
    }());
});